package com.supor.aiot.food.view;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: FoodGridItemDecoration <br>
 * 作者: lei <br>
 * 日期: 2021/1/29 <br>
 */
public class FoodGridItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int mDividerWidth;
    private int mDividerHeight;
    private int mBorderWidth;
    private int mBorderHeight;
    private int mExcludeViewType = -1;
    private boolean isDrawBorder = true;

    /**
     * @param color decoration line color.
     */
    public FoodGridItemDecoration(@ColorInt int color) {
        this(color, 2, 2, -1);
    }

    /**
     * @param color           line color.
     * @param dividerWidth    line width.
     * @param dividerHeight   line height.
     * @param excludeViewType don't need to draw the ViewType of the item of the split line.
     */
    public FoodGridItemDecoration(@ColorInt int color, int dividerWidth, int dividerHeight, int excludeViewType) {
        mDivider = new ColorDrawable(color);
        mDividerWidth = dividerWidth;
        mDividerHeight = dividerHeight;
        mExcludeViewType = excludeViewType;
    }

    /**
     * @param color           line color.
     * @param dividerWidth    line width.
     * @param dividerHeight   line height.
     */
    public FoodGridItemDecoration(@ColorInt int color, int dividerWidth, int dividerHeight, boolean isDrawBorder) {
        mDivider = new ColorDrawable(color);
        mDividerWidth = dividerWidth;
        mDividerHeight = dividerHeight;
        this.isDrawBorder = isDrawBorder;
    }
    public FoodGridItemDecoration(@ColorInt int color, int dividerWidth, int dividerHeight, int borderWidth, int borderHeight, int excludeViewType) {
        mDivider = new ColorDrawable(color);
        mDividerWidth = dividerWidth;
        mDividerHeight = dividerHeight;
        mBorderWidth = borderWidth;
        mBorderHeight = borderHeight;
        mExcludeViewType = excludeViewType;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildLayoutPosition(view);
        // Logc.e("position: " + position);

        if (position == mExcludeViewType) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
        // 获取item在span中的下标
        int spanIndex = params.getSpanIndex();
        int columnCount = getSpanCount(parent);

        boolean isFirstColumn = isFirstColumn(spanIndex, columnCount);
        boolean isLastColumn = isLastColumn(spanIndex, columnCount);
        // Logc.e("columnCount: " + columnCount + ", isFirstColumn: " + isFirstColumn + ", isLastColumn: " + isLastColumn);


        if (isFirstColumn) { // left, right, bottom
            outRect.set(mBorderWidth, mDividerHeight, mDividerWidth, mDividerHeight);
        } else if (isLastColumn) { // left, bottom
            outRect.set(mDividerWidth, mDividerHeight, mBorderHeight, mDividerHeight);
        } else { // left, bottom.
            outRect.set(mDividerWidth, mDividerHeight, mDividerWidth, mDividerHeight);
        }

        // modify by lei on 20210129
        // outRect.set(mDividerWidth, mDividerHeight, mDividerWidth, mDividerHeight);
    }

    private int getSpanCount(RecyclerView parent) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            return ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
        }
        return 1;
    }

    private boolean isFirstRaw(int position, int columnCount) {
        return position < columnCount;
    }

    private boolean isLastRaw(int position, int columnCount, int childCount) {
        if (columnCount == 1)
            return position + 1 == childCount;
        else {
            int lastRawItemCount = childCount % columnCount;
            int rawCount = (childCount - lastRawItemCount) / columnCount + (lastRawItemCount > 0 ? 1 : 0);

            int rawPositionJudge = (position + 1) % columnCount;
            if (rawPositionJudge == 0) {
                int rawPosition = (position + 1) / columnCount;
                return rawCount == rawPosition;
            } else {
                int rawPosition = (position + 1 - rawPositionJudge) / columnCount + 1;
                return rawCount == rawPosition;
            }
        }
    }

    private boolean isFirstColumn(int position, int columnCount) {
        if (position == 0)
            return true;
        return position % columnCount == 0;
    }

    private boolean isLastColumn(int position, int columnCount) {
        if (columnCount == 1)
            return true;
        return (position + 1) % columnCount == 0;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHorizontal(c, parent);
        drawVertical(c, parent);
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int left = child.getLeft();
            final int top = child.getBottom();
            final int right = child.getRight();
            final int bottom = top + mDividerHeight;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final int left = child.getRight();
            final int top = child.getTop();
            final int right = left + mDividerWidth;
            final int bottom = child.getBottom();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

}
